package top.glkj.hms.controller;

import com.sun.jndi.toolkit.url.UrlUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import top.glkj.hms.service.StaticValueService;
import top.glkj.hms.utils.Result;
import top.glkj.hms.utils.ResultFactory;
import top.glkj.hms.utils.StaticValue;
import top.glkj.hms.utils.Status;

import javax.annotation.security.PermitAll;
import java.net.MalformedURLException;
import java.util.HashMap;

/**
 * @author : xgl
 * @version : 0.0.1
 * @date :2020/11/30 1:57
 * 公共接口
 */
@Api(tags = "公共接口")
@RestController
@Slf4j
@PermitAll
@RequestMapping("/common")
public class CommonController {
    private final StaticValueService staticValueService;

    public CommonController(StaticValueService staticValueService) {
        this.staticValueService = staticValueService;
    }

    @ApiOperation("保存错误")
    @PermitAll
    @PostMapping(value = "/saveError")
    public void saveErrorLogController(@RequestBody String info) {
        try {
            log.error(UrlUtil.decode(info));
            System.out.println(UrlUtil.decode(info));
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
    }

    @ApiOperation("获取新增的用户")
    @GetMapping("/getNewUser")
    public Result getNewUser(){
        return ResultFactory.isSuccess(StaticValue.ZERO);
    }


    @ApiOperation("根据状态标识获取颜色值")
    @GetMapping("/status/{name}")
    public Result getStatusByName(@PathVariable(name = "name") String name){return ResultFactory.isSuccess(StaticValue.GET_DATA_SUCCESS,Status.valueOf(name).toString());}

    @ApiOperation("获取状态和颜色值")
    @GetMapping("/status")
    public Result getAllStatus(){
        HashMap<String,String> status = new HashMap<>();
        for(Status s:Status.values()){
            status.put(s.name(),s.toString());
        }
        return  ResultFactory.isSuccess(StaticValue.GET_DATA_SUCCESS,status);
    }

    public Result getStaticValueByName(String name){return staticValueService.getStaticValueByName(name);}
}
